<?php

namespace App\Jobs;

use App\Episode;
use App\Image;
use App\Jobs\Job;
use App\Show;
use GuzzleHttp\Client;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Symfony\Component\DomCrawler\Crawler;

class EditYyetstShowWithEpisodesJob extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;
    protected $episode;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Episode $episode)
    {
        $this->episode = $episode;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ($this->attempts() > 1) {
            $this->delete();
            return;
        }
        $episode = $this->episode;

        $url = $episode->url_provider;
        $url = str_ireplace('.zmz2017.com', '.zimuzu.tv', $url);


        $base_url = 'http://www.zimuzu.tv';
        $header_params = [
            'Host' => $base_url,
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'gzip, deflate, sdch',
            'Accept-Language' => 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
            'Cache-control:' => 'max-age=0',
            'Dnt' => '1',
            'Referer' => 'https://www.baidu.com/link?url=OOIVn0qbApPkoe99TFbeGI307BTpCTY4pXGJcll69PEQRzlZjqbxMmnq9DoI3AWQ&wd=&eqid=fe299461002fe7600000000557314a9e',
            'Upgrade-Insecure-Requests' => '1',
            'User-Agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
        ];
        //获取登陆cookie
        $client = new Client([
            'headers' => $header_params,
            'cookies' => true,
            'allow_redirects' => true,
            'base_uri' => $base_url
        ]);

        $res = $client->request('GET',$url);
        $dom = $res->getBody()->getContents();
        $crawler = new Crawler($dom);

        $show_name_en = $crawler->filter('body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-info > ul > li:nth-child(1) > strong')->first()->text();
        $show_name_en = str_ireplace('.',' ',$show_name_en);
        $show_name_en = trim($show_name_en);
        //var_dump($show_name_en);
        $show_image_url = $crawler->filter('body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-img > div.imglink > a > img')->first()->attr('src');
        //var_dump($show_image_url);

        $show_detail_text = $crawler->filter('body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.resource-desc > div:nth-child(3)')->first()->text();
        //var_dump($show_detail_text);

        $show_name_zh = $crawler->filter('body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-info > ul > li.mg')->first()->text();
        $show_name_zh = trim(str_ireplace('别名：','',$show_name_zh));

        //var_dump($show_name_zh);
        $show = Show::updateOrCreate(['name_en'=>$show_name_en],['name_zh'=>$show_name_zh,'key_word'=>$show_name_zh,'detail'=>$show_detail_text]);
        $show->timestamps = false;

        $key = date('Y_md_his') . $show_name_en;
        $alt = $show_name_en . $show_name_zh;
        $image = Image::fetchUrlWithKey($show_image_url,$key,$alt);
        $show->images()->attach($image->id);
        $show->episodes()->save($episode);
        var_dump($show->name_en,$show_name_en);
    }
}
